function loadMusicList()
{
	url='model/ajax/musiclist.php';
	$("#content").load(url,onceLoadedMusicList);
	$("#musiclist-category").hide();
}

function onceLoadedMusicList()
{
	$(".musiclist-choir-menu-button").click(
		function()
		{
			$("#musiclist-category").hide();
			$(".musiclist-choir-menu-button").removeClass("li-selected");
			$(this).addClass("li-selected");
			url='model/ajax/musiclist-reload.php';
			$.post
			(
				url,
				{choir:$(this).attr("id"),showAll:showAll},
				onceLoadedMusic 
			);
		}
	);
		
	$(".musiclist-choir-menu-button").hover(function(){$(this).addClass("li-hover");},function(){$(this).removeClass("li-hover");});

	$(".musiclist-actions-menu-button").hover(function(){$(this).addClass("li-hover");},function(){$(this).removeClass("li-hover");});
	$(".musiclist-actions-menu-button").click
	(
		function()
		{
			attrStr=$(this).attr("id");
//			alert("clicked: "+$attrStr);
			category=attrStr.split("-");
			cat = category[5];
			
			attrStr=$(".musiclist-choir-menu-button.li-selected").attr("id");
			choirArray = attrStr.split("-");
			choir = choirArray[4].substr(4);
			showMusicCategory(cat,choir);

		}
	);

	$(".musiclist-summary-menu-button").hover(function(){$(this).addClass("li-hover");},function(){$(this).removeClass("li-hover");});
	$(".musiclist-summary-menu-button").click
	(
		function()
		{
			attrStr=$(this).attr("id");
//			alert("clicked: "+$attrStr);
			eventID=attrStr.split("-");
			choir = eventID[5];

			window.open('model/ajax/musiclist-summary-printable.php?choir='+choir);
		}
	);
	
	$("#musiclist-startdate-button").click
	(
		function()
		{
			showAll = (showAll==true)?false:true;
			text = (showAll==true)?"Show from today":"Show all";
			$("#musiclist-startdate-button button").text(text);
			
/*			attrStr=$(".li-selected").attr("id");
			category=attrStr.split("-");
			cat = category[5];
*/			
			choir=$(".musiclist-choir-menu-button.li-selected").attr("id");
/*			choirArray = attrStr.split("-");
			choir = 'music-choir-menu-button-show' + choirArray[4].substr(4);*/

//			showMusicCategory(cat,choir);


			$("#musiclist-category").hide();
//			$(".musiclist-choir-menu-button").removeClass("li-selected");
//			$(this).addClass("li-selected");
			url='model/ajax/musiclist-reload.php';
			$.post
			(
				url,
				{choir:choir,showAll:showAll},
				onceLoadedMusic 
			);
		}
	);

	url='model/ajax/musiclist-reload.php';
	$.post
	(
		url,
		{choir:$(".musiclist-choir-menu-button.li-selected").attr("id"),showAll:showAll},
		onceLoadedMusic 
	);


	$("#content").slideDown(2000);
}

function showMusicCategory(cat,choir)
{
	
	url='model/ajax/musiclist-category.php';
	$.post
	(
		url,
		{
			choir:choir,
			category:cat
		},
		onceLoadedCategory
	);
}


function onceLoadedCategory(result)
{
	$("#musiclist-category").html(result);
	$("#musiclist-category").show();

}

function onceLoadedMusic(result)
{
	$("#musiclist").html(result);

	$("div.musiclist-table").click(
		function()
		{
			attrStr=$(this).attr("id");
//			alert("clicked: "+$attrStr);
			eventID=attrStr.split("-");
			id = eventID[2];
			editMusicEvent(id);
		});
}



function editMusicEvent(id)
{
	url='model/ajax/musiclist-edit.php';
	$.post(
		url,
		{eventID:id},
		onceLoadedEditMusicEvent);
}

function onceLoadedEditMusicEvent(data)
{
	
		$('#musiclist-edit').dialog
	(
		{
       		autoOpen: false,
       		modal: true,
       		title: "Details for Music List",
			height: 480,
			width: 450,
       		buttons: 
       		{
           		'OK' : function() {
           			url='model/ajax/musiclist-save.php';
					$.post
					(
						url,
						{
							eventid:$("#musiclist-edit-value-id").val(),
							date:$("#musiclist-edit-value-date").val(),
							time:$("#musiclist-edit-value-time").val(),
							choir:$("#musiclist-edit-value-choir").val(),
							note:$("#musiclist-edit-value-note").val(),
							service:$("#musiclist-edit-value-service").val(),
							psalms:$("#musiclist-edit-value-psalms").val(),
							hymns:$("#musiclist-edit-value-hymns").val(),
							introit:$("#musiclist-edit-value-introit").val(),
							responses:$("#musiclist-edit-value-responses").val(),
							mass:$("#musiclist-edit-value-mass").val(),
							canticles:$("#musiclist-edit-value-canticles").val(),
							anthem:$("#musiclist-edit-value-anthem").val()
						},
						onceLoadedSave
					);
//					$("#musiclist-edit").hide();
           			$(this).dialog('close');},
           			
       	    		'Cancel': function() {$(this).dialog('close');}
       		},
       		open: function(event,ui) 
       		{
				$("#musiclist-edit").html(data)
       		}
   		}
   	);
   
	$('#musiclist-edit').dialog('open');

	
	
	

	$(".musiclist-edit-value-text").click(
		function()
		{
			switch(this.id)
			{
				case 'musiclist-edit-value-introit':
				case 'musiclist-edit-value-responses':
				case 'musiclist-edit-value-mass':
				case 'musiclist-edit-value-canticles':
				case 'musiclist-edit-value-anthem':
//					alert(this.id)
					break;
					
				default:
					break;
				
			}

		});
	

	$("#musiclist-edit").show();
}

function onceLoadedSave(result)
{
	
	$(".li-selected").each
		(
		function()
			{
//				alert($(this).attr("id"));
				url='model/ajax/musiclist-reload.php';
				$.post
				(
					url,
					{choir:$(this).attr("id"),showAll:showAll},
					onceLoadedMusic
				);
			}
		);
	
	
/*	$(".musiclist-choir-menu-button").click(
		function()
		{
			$(".musiclist-choir-menu-button").removeClass("li-selected");
			$(this).addClass("li-selected");
			url='model/ajax/musiclist-reload.php';
			$.post
			(
				url,
				{choir:$(this).attr("id")},
				onceLoadedMusic
			);
		}
	);
		
	$(".musiclist-choir-menu-button").hover(function(){$(this).addClass("li-hover");},function(){$(this).removeClass("li-hover");});
	$("#content").slideDown(2000);*/
}
